package com.cskaoyan.service;

import com.cskaoyan.bean.bo.PageCommonParamBo;
import com.cskaoyan.bean.entity.Brand;
import com.cskaoyan.bean.entity.BrandExample;
import com.cskaoyan.bean.vo.BasePageData;
import com.cskaoyan.bean.vo.BaseParam;
import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.utils.LimitListSizeUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService{

    @Autowired
    BrandMapper brandMapper;


    // 查询所有的品牌制造商
    @Override
    public BasePageData<Brand> query(Integer id, String name, BaseParam baseParam) {
        // 拿到brandExample
        BrandExample brandExample = new BrandExample();
        // 获取 criterion对象，实现条件案例
        BrandExample.Criteria criteria = brandExample.createCriteria();

        // 配置分页信息
        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());

        //如果品牌的id不为空，就拼接条件 and id = id;
        if(id != null){
            criteria.andIdEqualTo(id);
        }
        // 品牌的名字不为空就拼接上名字
        if(name != null && !name.equals("")){
            criteria.andNameEqualTo(name);
        }
        //设置排序
        brandExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        // 根据条件查询brand
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        return BasePageData.ok(brands);
    }


    @Override
    public Brand add(Brand brandReceive) {
        // 拿到brandExample
        BrandExample brandExample = new BrandExample();
        // 获取 criterion对象，实现条件案例
        BrandExample.Criteria criteria = brandExample.createCriteria();

        // 添加
        Brand brand = new Brand();
        // 获取添加的时间
        Date date =new Date();

        // 赋值
        brand.setName(brandReceive.getName());
        brand.setDesc(brandReceive.getDesc());
        brand.setPicUrl(brandReceive.getPicUrl());
//      SortOrder 根据值正序排序
        brand.setSortOrder((byte)1);
        brand.setFloorPrice(brandReceive.getFloorPrice());
        brand.setAddTime(date);
        brand.setUpdateTime(date);
        brand.setDeleted(false);
        brandMapper.insert(brand);
        //返回添加的brand信息
        return  brand;
    }

    /**
     *  read方法，新增操作后，给新增后的品牌id赋值
     *
     */
    public Brand  read(Brand brand){
        // 拿到brandExample
        BrandExample brandExample = new BrandExample();
        // 获取 criterion对象，实现条件案例
        BrandExample.Criteria criteria = brandExample.createCriteria();
        // 判断接收到的参数
        // 前端必须要传入品牌的name，描述desc，最低价floorPrice,
        // 所以我们关键是要判断picUrl属性值是为空
        // 就是为了拿到唯一的id
        if(brand.getPicUrl() != null && !brand.getPicUrl().equals("")){
            // 拼接picUrl
            criteria.andPicUrlEqualTo(brand.getPicUrl());
        }
        // 拼接name
        if(brand.getName() != null){
            criteria.andNameEqualTo(brand.getName());
        }

        brandExample.setOrderByClause("update_time desc");
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        return brands.get(0);
    }

    @Override
    public Brand update(Brand updatedBrand) {
        // brandExample这里没有条件
        // 根据主键id修改，根据条件修改，会出现主键冲突的错误
        // 更新下修改时间
        updatedBrand.setUpdateTime(new Date());
        brandMapper.updateByPrimaryKeySelective(updatedBrand);
        return updatedBrand;
    }

    @Override
    public void delete(Brand brand) {
        // 根据主键id删除
        brandMapper.deleteByPrimaryKey(brand.getId());
    }


    /**
     * <h2> 小程序端品牌模块 wx/brand/list </h2>
     */
    @Override
    public BasePageData<Brand> list(PageCommonParamBo pageParam) {

        BrandExample brandExample = new BrandExample();
        //  设置排序
        brandExample.setOrderByClause("id desc");
        // 配置分页信息 根据条件查询brand
        PageHelper.startPage(pageParam.getPage(), pageParam.getLimit());
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        return BasePageData.ok(brands);
    }

    /**
     * <h2> 小程序端品牌模块 wx/brand/detail?id=1046040 </h2>
     */
    @Override
    public Brand detail(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     *  获取所有的品牌，限制显示四个
     *
     * @return
     */
    @Override
    public List<Brand> getAllBrand() {
        BrandExample brandExample = new BrandExample();
        List<Brand> brandList = brandMapper.selectByExample(brandExample);
        // 获取四个品牌
        return LimitListSizeUtil.limitSize(brandList);
    }
}
